﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RyanLam.NET.Core.ITemplates;
using RyanLam.NET.Core.Menu;
using WebComponents.Data;
using System.Data;
using System.Web;

namespace RyanLam.NET.Core.Controllers
{
   public static class GetMenus
    {
        /// <summary>
        /// Get all menu data.
        /// </summary>
        private static List<Core.Menu.Menu> getMenu = null;
        /// <summary>
        /// Get all menu data.
        /// </summary>
        public static void GetMenuData()
        {
            getMenu = null;
            List<Core.Menu.Menu> list = new List<Core.Menu.Menu>();
            string query = String.Format("SELECT * FROM Menu");
            DataTable dt = new DataTable();
            dt = GetData.DataTable(query, null);
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new Core.Menu.Menu
                {
                    ID = Convert.ToInt32(dr["ID"]),
                    ParentID = dr.IsNull("ParentID") ? 0 : Convert.ToInt32(dr["ParentID"]),
                    Text = dr.IsNull("Text") ? "[N/A]" : Convert.ToString(dr["Text"]),
                    CssClass = dr.IsNull("CssClass") ? "" : Convert.ToString(dr["CssClass"]),
                    Icon = dr.IsNull("Icon") ? "" : Convert.ToString(dr["Icon"]),
                    ToolTip = dr.IsNull("ToolTip") ? "[N/A]" : Convert.ToString(dr["ToolTip"]),
                    NavigationUrl = dr.IsNull("NavigationUrl") ? "[N/A]" : Convert.ToString(dr["NavigationUrl"]),
                    Target = dr.IsNull("Target") ? EnumTarget._self : (EnumTarget)Enum.Parse(typeof(EnumTarget), dr["Target"].ToString()),
                    Enabled = dr.IsNull("Enabled") ? true : Convert.ToBoolean(dr.IsNull("Enabled")),
                    Sort = dr.IsNull("Sort") ? 0 : Convert.ToInt32(dr["Sort"])                    
                });
                getMenu = list;
            }
        }
        /// <summary>
        /// Get menu.
        /// </summary>
        /// <param name="parentID">Menu's parent id.</param>
        /// <returns></returns>
        public static List<Core.Menu.Menu> GetMenu(int parentID)
        {
            if (getMenu == null)
                GetMenuData();

            var query = from t in getMenu
                        where t.ParentID.Equals(parentID)
                        orderby t.Sort ascending
                        select t;

            return query.ToList();
        }
        /// <summary>
        /// Get menu.
        /// </summary>
        /// <param name="parentID">Site menu's parent id.</param>
        /// <param name="sync">Reset data on memory.</param>
        /// <returns></returns>
        public static List<Core.Menu.Menu> GetMenu(int parentID, bool sync)
        {
            if (sync)
                GetMenuData();
            return GetMenu(parentID);
        }
        /// <summary>
        /// Get menu.
        /// </summary>
        /// <param name="parentID">Site menu's parent id.</param>
        /// <param name="sync">Reset data on memory.</param>
        /// <param name="checkPermit">Check allowed permit for current login user.</param>
        /// <returns></returns>
        public static List<Core.Menu.Menu> GetMenu(int parentID, bool sync, bool checkPermit)
        {
            List<Core.Menu.Menu> list = new List<Core.Menu.Menu>();
            if (sync)
                GetMenuData();
            if (!checkPermit)
            {
                list = GetMenu(parentID);
            }
            else
            {
                foreach (Core.Menu.Menu menu in GetMenu(parentID))
                {
                    if (menu.IsAllowed())
                        list.Add(menu);
                }
            }
            return list;
        }
    }
}
